package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.*;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblReceiveRefundAccountService;
import com.settlement.system.service.ITblReceiveRefundShopService;
import com.settlement.system.service.ITblRrChannelService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收退款-账号 前端控制器
 * @author Ximi
 * @since 2020-07-07
 */

@Api(tags = "基础配置-账号",description = "基础配置-账号")
@RestController
@RequestMapping("/receive_refund/account")
@Slf4j
public class TblReceiveRefundAccountController extends MergeApiExcelController<TblReceiveRefundAccount,Map<String,Object>> {

    @Resource
    private ITblReceiveRefundAccountService tblReceiveRefundAccountService;

    @Resource
    private ITblRrChannelService tblRrChannelService;

    @Resource
    private ITblReceiveRefundShopService tblReceiveRefundShopService;

    @Override
    public IBaseService init() {
        return tblReceiveRefundAccountService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblReceiveRefundAccount.class;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"shopNos");
        super.setParamsModel(model);
    }

    @Override
    public String verifyImportObject(TblReceiveRefundAccount tblReceiveRefundAccount, AnalysisContext analysisContext) {
        String s = super.verifyImportObject(tblReceiveRefundAccount, analysisContext);
        if(StringUtils.isNotEmpty(s)){
            return s;
        }
        if(!StringUtils.isEmpty(tblReceiveRefundAccount.getChannelName())){
            String channelNo = tblRrChannelService.getNoByName(tblReceiveRefundAccount.getChannelName());
            if(StringUtils.isEmpty(channelNo)){
                return "渠道名称不正确";
            }
            tblReceiveRefundAccount.setChannelNo(channelNo);
        }
        if(!tblReceiveRefundAccount.getChannelName().equals("线下打款") && StringUtils.isEmpty(tblReceiveRefundAccount.getShopNo())){
            return "零售店铺编码不能为空";
        }
        if(StringUtils.isNotEmpty(tblReceiveRefundAccount.getShopNo())){
            try {
                TblReceiveRefundShop shop = tblReceiveRefundShopService.getByShopNo(tblReceiveRefundAccount.getShopNo());
                if(shop!=null){
                    tblReceiveRefundAccount.setNcShopName(shop.getNcShopName());
                    tblReceiveRefundAccount.setNcShopNo(shop.getNcShopNo());
                    tblReceiveRefundAccount.setShopName(shop.getShopName());
                }
                TblReceiveRefundAccount account = tblReceiveRefundAccountService.selectExistAccount(tblReceiveRefundAccount);
                if(account!=null){
                    return "数据已存在";
                }

            } catch (ServiceException e) {
                log.error(e.getMessage(),e);
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    @ApiOperation("获取账户分页查询")
    @RequestMapping(value = "getNcVirtualAccount",method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse getNcVirtualAccount(HttpServletRequest request, @RequestBody BasePager<Map<String,Object>> model){
        Map<String,Object> result = new HashMap<>(16);
        try {
            int count = tblReceiveRefundAccountService.getNcVirtualAccountCount(model.getModel());
            List<TblReceiveRefundAccount> rows = tblReceiveRefundAccountService.getNcVirtualAccountList(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            result.put("rows",rows);
            result.put("total",count);
        } catch (Exception ex){
            log.error("获取列表出错",ex);
            return new ObjectDataResponse(500,"获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result,200,"获取列表成功");
    }

    @ApiOperation("获取账户名称分页查询")
    @RequestMapping(value = "getNcVirtualAccountName",method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse getNcVirtualAccountName(HttpServletRequest request, @RequestBody BasePager<Map<String,Object>> model){
        Map<String,Object> result = new HashMap<>(16);
        try {
            int count = tblReceiveRefundAccountService.getNcVirtualAccountNameCount(model.getModel());
            List<TblReceiveRefundAccount> rows = tblReceiveRefundAccountService.getNcVirtualAccountNameList(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            result.put("rows",rows);
            result.put("total",count);
        } catch (Exception ex){
            log.error("获取列表出错",ex);
            return new ObjectDataResponse(500,"获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result,200,"获取列表成功");
    }

    @Override
    protected String validateAllDate(List<TblReceiveRefundAccount> datas) {
        List<String> list = new ArrayList<>();
        for(TblReceiveRefundAccount data : datas){
            String toString = data.toUniqueString();
            if(list.contains(toString)){
                return "有两行数据重复";
            }else{
                list.add(toString);
            }
        }

        return super.validateAllDate(datas);
    }
}

